/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import dao.NguoidungDAO;
import dao.QuyenhanDAO;
import dao.TinhtrangDAO;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import pojo.Nguoidung;
import pojo.Quyenhan;
import pojo.Tinhtrang;

/**
 *
 * @author kydrenw
 */
public class NguoiDungController implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {

        hsr1.setCharacterEncoding("UTF-8");
        hsr.setCharacterEncoding("UTF-8");
        
        Nguoidung nguoiDung =(Nguoidung) hsr.getSession().getAttribute("nguoiDung");
        if (nguoiDung==null || !NguoidungDAO.laAdmin(nguoiDung.getTendangnhap()) ) {           
            hsr.setAttribute("tag", "no");
            return new ModelAndView("admin/index");
        }
        
        if (hsr.getParameter("action") != null) {
            String action = hsr.getParameter("action").toString();

            if (action.equals("sua")) {
                return suaNguoiDung(hsr, hsr1);
            }

            if (action.equals("sua_submit")) {
                return suaNguoiDung_Submit(hsr, hsr1);
            }

            if (action.equals("xoa")) {
                return xoaNguoiDung(hsr, hsr1);
            }

            if (action.equals("timkiem")) {
                return timKiem(hsr, hsr1);
            }

            if (action.equals("danhsach_xoa")) {
                // danh sach xoa
                return hienDanhSachNguoiDung(hsr, hsr1, 2);
            }

            if (action.equals("danhsach_cam")) {
                // danh sach cam
                return hienDanhSachNguoiDung(hsr, hsr1, 3);
            }
        }
        // danh sach dang hoat dong
        return hienDanhSachNguoiDung(hsr, hsr1, 1);

    }

    private ModelAndView hienDanhSachNguoiDung(HttpServletRequest hsr, HttpServletResponse hsr1, int type) throws Exception {
        hsr.setAttribute("tag", "nguoidung_danhsach");

        //List<Nguoidung> all = NguoidungDAO.layDanhSach(type);
        List<Nguoidung> all = NguoidungDAO.layDanhSach();

        int rows = 10;
        int totalPages = (int) Math.floor((all.size() - 1) / (float) rows) + 1;
        int pageNumber = 1;
        int fromIndex = 0;
        int toIndex = 0;

        if (hsr.getParameter("page") != null) {
            pageNumber = Integer.parseInt(hsr.getParameter("page"));
            if (pageNumber == totalPages) {
                fromIndex = (pageNumber - 1) * rows;
                toIndex = fromIndex + all.size()
                        - (rows * (totalPages - 1));
            } else {
                fromIndex = (pageNumber - 1) * rows;
                toIndex = fromIndex + rows;
            }
            List<Nguoidung> ds = all.subList(fromIndex, toIndex);
            hsr.setAttribute("ds", ds);
        } else {
            hsr.setAttribute("ds", all);
        }

        hsr.setAttribute("page", pageNumber);
        hsr.setAttribute("totalPages", totalPages);

        return new ModelAndView("admin/index");
    }

    private ModelAndView suaNguoiDung(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
        hsr.setAttribute("tag", "nguoidung_sua");
        String tendangnhap = hsr.getParameter("id");
        Nguoidung nguoidung = NguoidungDAO.layThongTin(tendangnhap);
        hsr.setAttribute("nguoidung", nguoidung);
        //2 : loai tinh trang nguoi dung
        List<Tinhtrang> dsTinhTrang = TinhtrangDAO.layDanhSach(2);
        hsr.setAttribute("dsTinhTrang", dsTinhTrang);

        List<Quyenhan> dsQuyenHan = QuyenhanDAO.layDanhSach();
        hsr.setAttribute("dsQuyenhan", dsQuyenHan);
        return new ModelAndView("admin/index");
    }

    private ModelAndView xoaNguoiDung(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
        String tendangnhap = hsr.getParameter("id");
        boolean rs = NguoidungDAO.xoaNguoidung(tendangnhap);

        return hienDanhSachNguoiDung(hsr, hsr1, 1);
    }

    private ModelAndView suaNguoiDung_Submit(HttpServletRequest hsr, HttpServletResponse hsr1) {
        hsr.setAttribute("tag", "nguoidung_sua");
        String tendangnhap = hsr.getParameter("txttendangnhap");
        Nguoidung nguoidung = NguoidungDAO.layThongTin(tendangnhap);
       
        int tinhtrang = Integer.parseInt(hsr.getParameter("tinhtrang"));
        Tinhtrang tt = TinhtrangDAO.layThongTin(tinhtrang);
        nguoidung.setTinhtrang(tt);

        List<Quyenhan> dsQuyenHan = QuyenhanDAO.layDanhSach();
        hsr.setAttribute("dsQuyenhan", dsQuyenHan);
        Set lQuyen = new HashSet();
        for (int i = 0; i < dsQuyenHan.size(); i++) {
            if (hsr.getParameter(dsQuyenHan.get(i).getMa().toString()) != null) {
                lQuyen.add(dsQuyenHan.get(i));
            }
        }
        nguoidung.setQuyenhans(lQuyen);
        NguoidungDAO.suaNguoiDung(nguoidung);
        //2 : loai tinh trang nguoi dung
        List<Tinhtrang> dsTinhTrang = TinhtrangDAO.layDanhSach(2);
        hsr.setAttribute("dsTinhTrang", dsTinhTrang);
        hsr.setAttribute("mess", "Cập nhật thành công");
        hsr.setAttribute("nguoidung", nguoidung);

        return new ModelAndView("admin/index");
    }

    private ModelAndView timKiem(HttpServletRequest hsr, HttpServletResponse hsr1) {
        hsr.setAttribute("tag", "nguoidung_danhsach");

        String tukhoa = hsr.getParameter("txttukhoa");
        int muc = Integer.parseInt(hsr.getParameter("muc"));
        List<Nguoidung> all = NguoidungDAO.timKiem(tukhoa, muc);

        int rows = 10;
        int totalPages = (int) Math.floor((all.size() - 1) / (float) rows) + 1;
        int pageNumber = 1;
        int fromIndex = 0;
        int toIndex = 0;

        if (hsr.getParameter("page") != null) {
            pageNumber = Integer.parseInt(hsr.getParameter("page"));
            if (pageNumber == totalPages) {
                fromIndex = (pageNumber - 1) * rows;
                toIndex = fromIndex + all.size()
                        - (rows * (totalPages - 1));
            } else {
                fromIndex = (pageNumber - 1) * rows;
                toIndex = fromIndex + rows;
            }
            List<Nguoidung> ds = all.subList(fromIndex, toIndex);
            hsr.setAttribute("ds", ds);
        } else {
            hsr.setAttribute("ds", all);
        }

        hsr.setAttribute("page", pageNumber);
        hsr.setAttribute("totalPages", totalPages);
        return new ModelAndView("admin/index");
    }
}